package com.supersystem.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

/**
 * <li>功能简述:图片缩放，拼接
 * <li>详细描述:
 * @author  luoyg
 * @version  [8.0 2011-10-10]
 * @see  [相关类/方法]
 * @since  8.0
 */

public class ImageUtil {

    /**
     * 默认图片宽:200
     */
    public static final int IMAGE_WIDTH = 200;
    /**
     * 默认图片高:100
     */
    public static final int IMAGE_HEIGHT = 100;

    /**
     * 拼接图片的后缀名
     */
    public static final String IMAGE_FORMAT_NAME = "GIF";

    /**
     * <li>方法描述：图片缩放
     * @param is
     * @param target
     */
    public static void scaleImage(InputStream is,String target){
        scaleImage(is, target,IMAGE_WIDTH,IMAGE_WIDTH);
    }

    /**
     * <li>方法描述：   图片缩放
     * @param is
     * @param target
     * @param w
     * @param h
     */
    public static void scaleImage(InputStream is,String target,int w,int h){
        try {
            BufferedImage srcImg = ImageIO.read(is);
            Image img = srcImg.getScaledInstance(w, h, BufferedImage.SCALE_DEFAULT);
            BufferedImage tagImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
            Graphics g = tagImg.getGraphics();
            g.drawImage(img, 0,0,null);
            g.dispose();
            ImageIO.write(tagImg, "PNG", new File(target));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * <li>方法描述：图片拼接
     * @param files
     * @param target
     */
    public static void spliceImages(File[] files,String target,int w,int h,String formatName){
        try {
            if(files != null && files.length > 0){
                File file = null;
                BufferedImage bufImg = null;
                int[][] imageArray = new int[files.length][w*h];
                int width = w;
                int height = h;

                BufferedImage newImage = new BufferedImage(w*files.length, h, BufferedImage.TYPE_INT_BGR);
                for (int i = 0; i < files.length; i++) {
                    file = files[i];
                    bufImg = ImageIO.read(file);
                    width = bufImg.getWidth();
                    height = bufImg.getHeight();

                    imageArray[i] = bufImg.getRGB(0, 0, width, height , imageArray[i], 0, width);

                    newImage.setRGB(i*w, 0, w, h,imageArray[i],0,w);
                }
                ImageIO.write(newImage, formatName, new File(target));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //测试
    public static void main(String[] args) {
        File dir = new File("C:/Documents and Settings/Administrator/My Documents/My Pictures/Google Talk");
        if(dir.exists()){
            File[] files = dir.listFiles();
            File[] f = new File[2];
            f[0] = files[1];
            try {
                scaleImage(new FileInputStream(f[0]), "e:/test.png");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            /*f[1] = files[1];
            File newFile = new File("e:/test.png");
            if(newFile.exists())newFile.delete();
            try {
                newFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            spliceImages(f, newFile.getAbsolutePath(), 200, 200, "PNG");*/
        }
    }
}
